<%@ include file="header.jsp" %>
<%
	String message = request.getParameter("message");
	String error = request.getParameter("error");
	if (request.getParameter("service") != null) {
		com.googlecode.jwsm.Service service = com.googlecode.jwsm.ServiceManager.getInstance().getService(request.getParameter("service"));
		String action = request.getParameter("action");
		if ((service != null) && (action != null)) {
	if ((action.equalsIgnoreCase("stop")) && (user.hasPrivilege("jwsm.service_manage"))) {
		service.stop();
		message = "Service \"" + service.getName() + "\" stopped successfully.";
	} else if ((action.equalsIgnoreCase("start")) && (user.hasPrivilege("jwsm.service_manage"))) {
		service.start();
		message = "Service \"" + service.getName() + "\" started successfully.";
	} else if ((action.equalsIgnoreCase("restart")) && (user.hasPrivilege("jwsm.service_manage"))) {
		service.stop();
		service.start();
		message = "Service \"" + service.getName() + "\" restarted successfully.";
	} else if ((action.equalsIgnoreCase("undeploy")) && (user.hasPrivilege("jwsm.service_delete"))) {
		error = service.undeploy();
		if (error == null) {
	message = "Service \"" + service.getName() + "\" undeployed successfully.";
		}
	}
		}
	}
%>
<%@page import="java.io.File"%>
<%@page import="org.jcommon.util.StringUtilities"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="com.googlecode.jwsm.*"%>
<%
	if (message != null) {
%>
	<b style="color: blue;"><%=message%></b><br/><br/>
<%
	}
%>
<%
	if (error != null) {
%>
	<b style="color: red;"><%=error%></b><br/><br/>
<%
	}
%>
<span style="color: <%= CSSDefaults.BG_COLOR %>; font-size: 14pt; font-weight: bold;">Service List</span>
<br/><br/>
<table class="sortable" id="ServiceTable" cellpadding="4" cellspacing="0" style="border: 1px solid #000000;">
	<tr bgcolor="<%= CSSDefaults.BG_COLOR %>">
		<td id="ServiceName" class="table">Service Name</td>
		<td id="ServiceStatus" class="table">Status</td>
		<td id="ServiceRunningTime" class="table">Running Since</td>
		<td id="ServiceRequestCount" class="table">Requests</td>
		<td id="ServiceRequestFaults" class="table">Faults</td>
		<td id="ServiceActions" class="table">Actions</td>
	</tr>
<%
	com.googlecode.jwsm.Service[] services = com.googlecode.jwsm.ServiceManager.getInstance().getServices();
	for (int i = 0; i < services.length; i++) {
%>
	<tr>
		<td class="table"><%= services[i].getName() %><% if (services[i].isDebug()) { %> (<i style="color: red;">debug</i>)<% } %></td>
		<td class="table"><%= services[i].getStatus() %></td>
		<td class="table"><%= services[i].getStarted() %></td>
		<td class="table" align="right"<% if ((services[i].getRequestCount() > 0) && (services[i].getRequestCount() == services[i].getErrorCount())) { %> bgcolor="red"<% } %>><%= services[i].getRequestCount() %></td>
		<td class="table" align="right"<% if ((services[i].getRequestCount() > 0) && (services[i].getRequestCount() == services[i].getErrorCount())) { %> bgcolor="red"<% } %>><%= services[i].getErrorCount() %></td>
		<td class="table"><select onchange="executeAction('<%= services[i].getName() %>', this);">
							<option>-- Execute --</option>
							<% if (services[i].isStarted()) { %>
								<% if (user.hasPrivilege("jwsm.service_manage")) { %><option value="Restart">Restart</option><% } %>
								<% if (user.hasPrivilege("jwsm.service_manage")) { %><option value="Stop">Stop</option><% } %>
								<option value="Test">Test</option>
								<option value="Generate Client">Generate Client</option>
							<% } else { %>
								<% if (user.hasPrivilege("jwsm.service_manage")) { %><option value="Start">Start</option><% } %>
							<% } %>
							<% if (user.hasPrivilege("jwsm.service_manage")) { %><option value="Configure">Configure</option><% } %>
							<% if (user.hasPrivilege("jwsm.service_delete")) { %><option value="Undeploy">Undeploy</option><% } %>
						</select></td>
	</tr>
<%
	}
	if (services.length == 0) {
%>
	<tr>
		<td class="table" colspan="6" align="center"><i>No services defined</i></td>
	</tr>
<%
	}
%>
</table>
<br/><br/>
<span style="color: <%= CSSDefaults.BG_COLOR %>; font-size: 14pt; font-weight: bold;">XSLT List</span>
<br/><br/>
<form id="xsltForm" action="XSLTServlet" method="POST" enctype="multipart/form-data">
<table class="sortable" id="XSLTTable" cellpadding="4" cellspacing="0" style="border: 1px solid #000000;">
	<tr bgcolor="<%= CSSDefaults.BG_COLOR %>">
		<td id="XSLTNameColumn" class="table">Name</td>
		<td id="XSLTFileColumn" class="table">Filename</td>
		<td id="XSLTTypeColumn" class="table">Type</td>
		<td id="XSLTAssociationColumn" class="table">Default Association</td>
		<td id="XSLTActionsColumn" class="table">Actions</td>
	</tr>
<%
	int totalXSLTs = 0;

	XSLT[] xslts = ServiceManager.getInstance().getOutgoingXSLTs();
	totalXSLTs += xslts.length;
	for (int i = 0; i < xslts.length; i++) {
%>
	<tr>
		<td class="table"><%= xslts[i].getName() %></td>
		<td class="table"><%= xslts[i].getFilename() %></td>
		<td class="table">Outgoing</td>
		<td class="table"><% if (xslts[i].getDefaultAssociation() != null) { %><%= xslts[i].getDefaultAssociation() %><% } else { %><i>No default association</i><% } %></td>
		<td class="table"><select onchange="executeAction('Eval', this);">
							<option>-- Execute --</option>
							<option value="document.location = 'XSLTServlet?xslt=<%= xslts[i].getName() %>&action=download&type=<%= xslts[i].getType() %>';">Download</option>
							<% if (user.hasPrivilege("jwsm.xslt_manage")) { %><option value="if (confirm('Are you sure you want to delete this XSLT?')) { document.location = 'XSLTServlet?xslt=<%= xslts[i].getName() %>&action=delete&type=<%= xslts[i].getType() %>'; }">Delete</option><% } %>
						</select></td>
	</tr>
<%
	}
	
	xslts = ServiceManager.getInstance().getIncomingXSLTs();
	if (xslts != null) {
		totalXSLTs += xslts.length;
		for (int i = 0; i < xslts.length; i++) {
%>
	<tr>
		<td class="table"><%= xslts[i].getName() %></td>
		<td class="table"><%= xslts[i].getFilename() %></td>
		<td class="table">Incoming</td>
		<td class="table"><% if (xslts[i].getDefaultAssociation() != null) { %><%= xslts[i].getDefaultAssociation() %><% } else { %><i>No default association</i><% } %></td>
		<td class="table"><select onchange="executeAction('Eval', this);">
							<option>-- Execute --</option>
							<option value="document.location = 'XSLTServlet?xslt=<%= xslts[i].getName() %>&action=download&type=<%= xslts[i].getType() %>';">Download</option>
							<% if (user.hasPrivilege("jwsm.xslt_manage")) { %><option value="if (confirm('Are you sure you want to delete this XSLT?')) { document.location = 'XSLTServlet?xslt=<%= xslts[i].getName() %>&action=delete&type=<%= xslts[i].getType() %>'; }">Delete</option><% } %>
						</select></td>
	</tr>
<%
		}
	
		if (totalXSLTs == 0) {
%>
	<tr>
		<td class="table" colspan="5" align="center"><i>No XSLTs defined</i></td>
	</tr>
<%
		}
	}
	types = ServiceManager.getInstance().getServiceReturnTypes();
	
	if (user.hasPrivilege("jwsm.xslt_manage")) {
%>
<tr>
	<td class="table"><input type="text" name="name"/></td>
	<td class="table"><input type="file" name="xslt"/></td>
	<td class="table"><select id="xsltType" name="type" onchange="changeXSLTType();"><option value="outgoing">Outgoing</option><option value="incoming">Incoming</option></select></td>
	<td class="table">
		<select id="defaultClass" name="defaultClass">
			<option value="">&lt;no default association&gt;</option>
			<% for (int i = 0; i < types.length; i++) { %>
				<option value="<%= types[i] %>"><%= types[i] %></option>
			<% } %>
		</select>
	</td>
	<td class="table" align="center"><input type="submit" value="Create"/></td>
</tr>
<%
	}
%>
</table>
</form>
<br/><br/>
<span style="color: <%= CSSDefaults.BG_COLOR %>; font-size: 14pt; font-weight: bold;">WSAR List</span>
<br/><br/>
<table class="sortable" id="WSARTable" cellpadding="4" cellspacing="0" style="border: 1px solid #000000;">
	<tr bgcolor="<%= CSSDefaults.BG_COLOR %>">
		<td id="WSARFile" class="table">Filename</td>
		<td id="WSARSize" class="table">Size</td>
		<td id="WSARUpdated" class="table">Last Updated</td>
		<td id="WSARActions" class="table">Actions</td>
	</tr>
<%
	File[] wsars = Uploader.getWSARs();
	GregorianCalendar calendar = new GregorianCalendar();
	if (wsars != null) {
		for (int i = 0; i < wsars.length; i++) {
			calendar.setTimeInMillis(wsars[i].lastModified());
%>
	<tr>
		<td class="table"><%= wsars[i].getName() %></td>
		<td class="table"><%= StringUtilities.format(wsars[i].length(), StringUtilities.FILE_FORMAT) %></td>
		<td class="table"><%= StringUtilities.format(calendar, "%MMMM% %d%, %yyyy% %hh%:%mm%:%ss%%amPM%") %></td>
		<td class="table"><select onchange="executeAction('<%= wsars[i].getName() %>', this);">
							<option>-- Execute --</option>
							<option value="Download">Download</option>
							<% if (user.hasPrivilege("jwsm.wsar_manage")) { %><option value="Deploy">Deploy</option><% } %>
							<% if (user.hasPrivilege("jwsm.wsar_manage")) { %><option value="Delete">Delete</option><% } %>
						</select></td>
	</tr>
<%
		}
	}
	if ((wsars == null) || (wsars.length == 0)) {
%>
	<tr>
		<td class="table" colspan="4" align="center"><i>No WSARs found</i></td>
	</tr>
<%
	}
%>
</table>
<%@ include file="footer.jsp" %>